<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="740px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="false"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改套餐' : '添加套餐'"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">

      <el-form-item label="名称:" prop="name">
        <el-input
          clearable
          v-model="form.name"
          placeholder="请输入名称"
        />
      </el-form-item>

      <el-form-item  label="价格:" prop="money">
        <el-input
          v-model="form.money"
          placeholder="请输入价格"
        />
      </el-form-item>

      <el-form-item  label="天数:" prop="day" v-if="type == 1">
        <el-input-number
          v-model="form.day"
          placeholder="请输入天数"
        />
      </el-form-item>



      <el-form-item label="是否无限使用:" prop="is_infinite">
        <el-switch
          :active-value="1"
          :inactive-value="0"
          v-model="form.is_infinite"
        />
        <el-tooltip
          placement="top"
          content="选择无限使用则账号不限制次数"
        >
          <i
            class="el-icon-_question"
            style="vertical-align: middle; margin-left: 8px"
          ></i>
        </el-tooltip>
      </el-form-item>


      <el-form-item  label="形象克隆次数:" prop="avatar_twin_count" v-if="form.is_infinite == 0 && type == 1">
        <el-input-number
          v-model="form.avatar_twin_count"
          placeholder="请输入形象克隆次数"
        />
      </el-form-item>

      <el-form-item  label="声音克隆次数:" prop="voice_twin_count" v-if="form.is_infinite == 0 && type == 1">
        <el-input-number
          v-model="form.voice_twin_count"
          placeholder="请输入声音克隆次数"
        />
      </el-form-item>

      <el-form-item  label="视频剪辑时长:" prop="video_clip_count" v-if="form.is_infinite == 0 && type == 1">
        <el-input-number
          v-model="form.video_clip_count"
          placeholder="请输入视频剪辑时长"
        />
      </el-form-item>

      
      <el-form-item  label="点数:" prop="point" v-if="type == 2">
        <el-input-number
          v-model="form.point"
          placeholder="请输入点数"
        />
      </el-form-item>

      <el-form-item  label="描述:" prop="desc">
        <el-input
          v-model="form.desc"
          placeholder="请输入描述"
        />
      </el-form-item>

      <el-form-item label="排序号:" prop="sort">
        <el-input-number
          :min="0"
          v-model="form.sort"
          placeholder="请输入排序号"
          controls-position="right"
          class="ele-fluid ele-text-left"
        />
      </el-form-item>

     
    

      <el-form-item label="展示状态:">
        <el-switch
          :active-value="1"
          :inactive-value="2"
          v-model="form.is_display"
        />
        <el-tooltip
          placement="top"
          content="选择不可见则不可选择该数据"
        >
          <i
            class="el-icon-_question"
            style="vertical-align: middle; margin-left: 8px"
          ></i>
        </el-tooltip>
      </el-form-item>

       
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消 </el-button>
      <el-button type="primary" :loading="loading" @click="save"
        >保存
      </el-button>
    </template>
    
  </ele-modal>
</template>

<script>
  import { add,update } from '@/api/package/list';
  const DEFAULT_FORM = {
    id: 0,
    name: '',
    type :1,
    is_infinite :0,
    money:0,
    day:0,
    avatar_twin_count:0,
    voice_twin_count:0,
    video_clip_count:0,
    point:0,
    desc: '',
    is_display :1,
    sort :1
  };

  export default {
    name: 'PackageEdit',
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
      type:0
    },
    data() {
      return {
        // 表单数据
        form: { ...DEFAULT_FORM },
        // 表单验证规则
        rules: {
          name: [
            {
              required: true,
              message: '请输入套餐标题',
              trigger: 'blur'
            }
          ],
          username: [
            {
              required: true,
              message: '请输入套餐账号',
              trigger: 'blur'
            }
          ],
          password: [
            {
              required: true,
              message: '请输入套餐密码',
              trigger: 'blur'
            }
          ],
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
      };
    },
    methods: {
      /* 保存编辑 */
      save() {
        this.$refs['form'].validate((valid) => {
          if (!valid) {
            return false;
          }

          this.loading = true;
          const data = {
            ...this.form,
          };

          data.type = this.type;

          const saveOrUpdata = this.isUpdate ? update : add;

          saveOrUpdata(data).then((msg) => {
            this.loading = false;
            this.$message.success(msg);
            this.updateVisible(false);
            this.$emit('done',{ type: this.type });
          }).catch((e) => {
            this.loading = false;
            this.$message.error(e.message);
          });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {

            this.$util.assignObject(this.form, {
              ...this.data
            });
            this.isUpdate = true;
          } else {
            this.isUpdate = false;
          }
        } else {
          this.$refs['form'].clearValidate();
          this.form = { ...DEFAULT_FORM };
        }
      }
    }
  };
</script>
